#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class RoomRecordProvider : CY.GFive.Core.Providers.DALProvider.IRoomRecordProvider
	{
		#region Sql
		private static readonly string SqlSelectRoomRecord = "SELECT [Id], [Content],[RcdCate],[IssueDate],[HandlerCode],[HandlerName],[HandlerView],[RcdDate],[RoomCode] FROM [RoomRecord] WHERE  [Id]=@Id";

		private static readonly string SqlInsertRoomRecord = "INSERT INTO [RoomRecord]([Content],[RcdCate],[IssueDate],[HandlerCode],[HandlerName],[HandlerView],[RcdDate],[RoomCode]) values(@Content,@RcdCate,@IssueDate,@HandlerCode,@HandlerName,@HandlerView,@RcdDate,@RoomCode);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateRoomRecord = "UPDATE [RoomRecord] SET [Content] = @Content,[RcdCate] = @RcdCate,[IssueDate] = @IssueDate,[HandlerCode] = @HandlerCode,[HandlerName] = @HandlerName,[HandlerView] = @HandlerView,[RcdDate] = @RcdDate,[RoomCode] = @RoomCode WHERE  [Id]=@Id";

		private static readonly string SqlDeleteRoomRecord = "DELETE [RoomRecord] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllRoomRecord = "SELECT [Id], [Content],[RcdCate],[IssueDate],[HandlerCode],[HandlerName],[HandlerView],[RcdDate],[RoomCode] FROM [RoomRecord]";
		#endregion

		#region IRoomRecordProvider Members
		public Core.Business.RoomRecord Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectRoomRecord);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.RoomRecord roomRecord = new Core.Business.RoomRecord();

				if (!reader.IsDBNull(0)) roomRecord.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) roomRecord.Content = reader.GetString(1);
				if (!reader.IsDBNull(2)) roomRecord.RcdCate = reader.GetString(2);
				if (!reader.IsDBNull(3)) roomRecord.IssueDate = reader.GetDateTime(3);
				if (!reader.IsDBNull(4)) roomRecord.HandlerCode = reader.GetString(4);
				if (!reader.IsDBNull(5)) roomRecord.HandlerName = reader.GetString(5);
				if (!reader.IsDBNull(6)) roomRecord.HandlerView = reader.GetString(6);
				if (!reader.IsDBNull(7)) roomRecord.RcdDate = reader.GetDateTime(7);
				if (!reader.IsDBNull(8)) roomRecord.RoomCode = reader.GetString(8);

				return roomRecord;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.RoomRecord roomRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Content", SqlDbType.NVarChar, roomRecord.Content);
			sql.AddParameter("@RcdCate", SqlDbType.NVarChar, roomRecord.RcdCate);
			sql.AddParameter("@IssueDate", SqlDbType.DateTime, roomRecord.IssueDate);
			sql.AddParameter("@HandlerCode", SqlDbType.NVarChar, roomRecord.HandlerCode);
			sql.AddParameter("@HandlerName", SqlDbType.NVarChar, roomRecord.HandlerName);
			sql.AddParameter("@HandlerView", SqlDbType.NVarChar, roomRecord.HandlerView);
			sql.AddParameter("@RcdDate", SqlDbType.DateTime, roomRecord.RcdDate);
			sql.AddParameter("@RoomCode", SqlDbType.NVarChar, roomRecord.RoomCode);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertRoomRecord);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) roomRecord.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.RoomRecord roomRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Content", SqlDbType.NVarChar, roomRecord.Content);
			sql.AddParameter("@RcdCate", SqlDbType.NVarChar, roomRecord.RcdCate);
			sql.AddParameter("@IssueDate", SqlDbType.DateTime, roomRecord.IssueDate);
			sql.AddParameter("@HandlerCode", SqlDbType.NVarChar, roomRecord.HandlerCode);
			sql.AddParameter("@HandlerName", SqlDbType.NVarChar, roomRecord.HandlerName);
			sql.AddParameter("@HandlerView", SqlDbType.NVarChar, roomRecord.HandlerView);
			sql.AddParameter("@RcdDate", SqlDbType.DateTime, roomRecord.RcdDate);
			sql.AddParameter("@RoomCode", SqlDbType.NVarChar, roomRecord.RoomCode);
			sql.AddParameter("@Id", SqlDbType.Int, roomRecord.Id);
			sql.ExecuteSql(SqlUpdateRoomRecord);
		}

		public void Delete(Core.Business.RoomRecord roomRecord)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, roomRecord.Id);
			sql.ExecuteSql(SqlDeleteRoomRecord);
		}

		public IList<Core.Business.RoomRecord> GetAllRoomRecord()
		{
			IList<Core.Business.RoomRecord> roomRecordlist = new List<Core.Business.RoomRecord>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllRoomRecord);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.RoomRecord roomRecord = new Core.Business.RoomRecord();

					if (!reader.IsDBNull(0)) roomRecord.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) roomRecord.Content = reader.GetString(1);
					if (!reader.IsDBNull(2)) roomRecord.RcdCate = reader.GetString(2);
					if (!reader.IsDBNull(3)) roomRecord.IssueDate = reader.GetDateTime(3);
					if (!reader.IsDBNull(4)) roomRecord.HandlerCode = reader.GetString(4);
					if (!reader.IsDBNull(5)) roomRecord.HandlerName = reader.GetString(5);
					if (!reader.IsDBNull(6)) roomRecord.HandlerView = reader.GetString(6);
					if (!reader.IsDBNull(7)) roomRecord.RcdDate = reader.GetDateTime(7);
					if (!reader.IsDBNull(8)) roomRecord.RoomCode = reader.GetString(8);

					roomRecord.MarkOld();
					roomRecordlist.Add(roomRecord);
				}
				reader.Close();
			}
			return roomRecordlist;
		}
		#endregion
	}
}
